<?php

class CategoryEX_Functions extends NP_CategoryEX
{

var $ecatid;

	function CategoryEX_Functions()
	{
		global $CONF;
		if (empty($this->ecatid)) {
			if ($CONF['URLMode'] == 'pathinfo') {
				$sid = 0;
				$pathdata = explode("/", serverVar('REQUEST_URI'));
				for ($i=0;$i<count($pathdata);$i++) {
					switch ($pathdata[$i]) {
						case $this->getRequestName():
							$i++;
							if ($i<count($pathdata)) $sid = $pathdata[$i];
							break 2;
					}
				}
				if ($sid) $this->ecatid = intval($sid);
			} else {
				$this->ecatid = intRequestVar($this->getRequestName());
			}
		}
	}

	function reSortTree($treePart)
	{
		if (!is_array($treePart)) {
			return;
		}
		uasort($treePart, array(&$this, 'sortCatOrder'));
		foreach ($treePart as $value) {
			$this->treeData[] = $value;
			if (   in_array($value['pnode'], $this->treeKeys)
				&& !empty($this->treeParts[$value['node']])
				&& is_array($this->treeParts[$value['node']])  ) {
					$this->reSortTree($this->treeParts[$value['node']]);
					unset($this->treeParts[$value['node']]);
			}
		}
		return true;
	}

	function sortCatOrder($aPart, $bPart)
	{
		if ($aPart['corder'] < $bPart['corder']) {
			return false;
		} elseif (		$aPart['corder'] == $bPart['corder']
					&&  $aPart['pcatid'] <  $bPart['pcatid'] ) {
			return false;
		} elseif (		$aPart['corder'] == $bPart['corder']
					&&  $aPart['pcatid'] == $bPart['pcatid']
					&&  $aPart['orgid']  <  $bPart['orgid']  ) {
			return false;
		}
		return true;
	}

	function getBlogData($bid)
	{
		global $manager;
		$b  =& $manager->getBlog(intval($bid));
		$parts = array(
			'maximum' => 'tright',
			'where'   => array(
				'blogid' => array('=', $blogid),
			)
		);
		$maxRight = $this->dbh->getMinMaxColumnIntArray(_NP_CATEGORYEX_TREE_TABLE, $parts);
		$rootNode = array(
			'id'     => 0,
			'bid'    => $id,
			'pclass' => 'nucleus',
			'pid'    => 0,
			'alias'  => 'blog',
			'orgid'  => $id,
			'tleft'  => 0,
			'tright' => $maxRight['maximumtright'] + 1,
			'cname'  => $b->settings['bname'],
			'cdesc'  => $b->settings['bdesc'],
			'corder' => 0,
		);
		return array($rootNode);
	}

	function getTreeDataArray($res, $bData = false, $level = false)
	{
		global $manager;
		$this->treeKeys  = array();
		$this->treeParts = array();
		$this->treeData  = array();
		if ($bData) {
			$tree = array_merge($bData, $res);
		} else {
			$tree = $res;
		}
		$right = array();
		$count = 0;
		$base  = 0;
		$tmp = array();
		foreach ($tree as $row) {
			if (count($right) > 0) {
				while ($right[count($right) - 1] < $row['tright']) {
					array_pop($right);
				}
			}
			$depth = count($right);// + 1;
			if (!$level || $depth <= $level) {
				if ($row['alias'] == 'subcat') {
					$param = array($this->subrequest => $row['orgid']);
					$cid   = $row['pcatid'];
				} elseif ($row['alias'] == 'cat') {
					$param = '';
					$cid   = $row['orgid'];
				}
				$pnode = $row['pclass'] . $row['pid'];
				$node  = $row['alias'] . $row['orgid'];
				$pnode = -1;
				if ($count > 0) {
					foreach ($nodeList as $key => $value) {
						if ($value == $row['pclass'] . $row['pid']) {
							$pnode = $key;
						}
					}
				}
				$this->treeParts[$pnode][$count]          = $row;
				$this->treeParts[$pnode][$count]['level'] = $depth;
				$this->treeParts[$pnode][$count]['node']  = $count;
				$this->treeParts[$pnode][$count]['pnode'] = $pnode;
				$this->treeParts[$pnode][$count]['count'] = $count;
				$this->treeParts[$pnode][$count]['link']  = createCategoryLink($cid, $param);
				$right[]                                  = $row['tright'];
				$nodeList[$count]                         = $row['alias'] . $row['orgid'];
				$this->treeKeys[]                         = $pnode;
			}
			$count++;
		}
		$temp = $this->treeKeys;
		for ($i=0;$i<count($this->treeKeys);$i++) {
			$this->reSortTree($this->treeParts[$this->treeKeys[$i]]); 
		}
		if ($this->treeData[0]['alias'] == 'blog') {
			$this->treeData[0]['link'] = createBlogidLink($this->treeData[0]['orgid']);
		}
		return $this->treeData;
	}

// -------------------------  pubric functions start  -------------------------

	function getRequestName()
	{
		return 'ecatid';
	}

	function getBlogIDFromEcatID($id)
	{
		$parts = array(
			'column' => 'blogid',
			'where'  => array(
				'ecatid' => array('=', $id),
			)
		);
		$ibd = $this->dbh->resultOne($table, $parts);
		return intval($bid);
	}

	function getParentsTree($id, $level = false, $bFlag = false)
	{
		$parts = array(
			'column' => '*',
			'where'  => array(
				'ecatid' => array('=', $id),
			)
		);
		$ecat  = $this->dbh->getSingleRowAssoc(_NP_CATEGORYEX_TREE_TABLE, $parts);
		$parts = array(
			'column' => '*',
			'where'  => array(
				'tleft'  => array('<=', $ecat['tleft']),
				'tright' => array('>=', $ecat['tright']),
				'blogid' => array('=',  $ecat['blogid']),
			),
			'order'  => array(
				'tleft' => 'ASC'
			)
		);
		$tmpTree = $this->dbh->getAllRowAssoc(_NP_CATEGORYEX_TREE_TABLE, $partsre);
		$depth   = count($tmpTree);
		if ($level && ($level <= $depth)) {
				$depth = $depth - $level;
				$trush = array_splice($tmpTree, 0, $depth);
		} elseif ((!$level || ($level > $depth)) && $bFlag) {
			$bData = $this->getBlogData($ecat['blogid']);
		}
		return $this->getTreeDataArray($tmpTree, $bData);
	}

	function getChildrenTree($id, $level = false, $bflag = true, $flg = ture)
	{
		$bData = false;
		if ($bflag) {
			$bData = $this->getBlogData($id);
			$ecat  = $bData;
		} else {
			$parts = array(
				'column' => 'blogid',
				'where'  => array(
					'ecatid' => array('=', $id),
				)
			);
			$ecat = $this->dbh->getSingleRowAssoc(_NP_CATEGORYEX_TREE_TABLE, $parts);
		}
		$parts = array(
			'column' => '*',
			'where'  => array(
				'tleft'  => array('<=', $ecat['tleft']),
				'tright' => array('>=', $ecat['tright']),
				'blogid' => array('=',  $ecat['blogid']),
			),
			'order'  => array(
				'tleft' => 'ASC'
			)
		);
		$res = $this->dbh->getAllRowAssoc(_NP_CATEGORYEX_TREE_TABLE, $partsre);
		if ($flg) {
			return $this->getTreeDataArray($res, $bData, $level);
		} else {
			return $res;
		}
	}










}

